package com.gyg.controller;

import cn.hutool.json.JSONUtil;
import com.gyg.client.CloudMsgClient;
import com.gyg.client.CloudUserClient;
import com.gyg.domain.CloudUser;
import com.gyg.enums.SessionEnum;
import com.gyg.returns.RespCode;
import com.gyg.returns.RespEntity;
import com.gyg.utils.OSSUtil;
import com.gyg.vo.param.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 短信验证码表 前端控制器
 * </p>
 *
 * @author gyg
 * @since 2021-02-20
 */
@RestController
@RequestMapping("/cloud-user")
@Slf4j
@Api(value = "用户信息接口", tags = "用户信息接口")
public class CloudUserController {

    @Resource
    private CloudUserClient cloudUserClient;

    @Resource
    private OSSUtil ossUtil;

    @GetMapping("/check")
    @ApiOperation(value = "校验账号")
    @ApiImplicitParam(paramType = "query", dataType = "String", name = "username", value = "账号", required = true)
    public RespEntity checkUsername(@RequestParam(value = "username", required = true) String username) {
        RespEntity respEntity = cloudUserClient.checkUsername(username);
        return respEntity;
    }

    @PostMapping("/register")
    @ApiOperation(value = "新用户注册")
    public RespEntity register (RegisterParam registerParam) {
        RespEntity respEntity = cloudUserClient.register(registerParam);
        return respEntity;
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登陆")
    public RespEntity<CloudUser> login(LoginParam loginParam, HttpSession session) {
        RespEntity<CloudUser> respEntity = cloudUserClient.login(loginParam);
        if (Objects.isNull(respEntity.getData())) {
            return new RespEntity(RespCode.LOGIN_ERROR);
        }
        Map<String, Object> map = (Map<String, Object>) respEntity.getData();
        CloudUser cloudUser = JSONUtil.toBean(JSONUtil.toJsonStr(map), CloudUser.class);
        if (!cloudUser.getBan().equals(0)) {
            return new RespEntity<>(RespCode.IS_BAN);
        }
        session.setAttribute(SessionEnum.USER.getCode(), cloudUser);
        session.removeAttribute(SessionEnum.BLOCK.getCode());
        return new RespEntity(RespCode.SUCCESS);
    }

    @GetMapping("/welcome")
    @ApiOperation(value = "首页欢迎语")
    public RespEntity welcome(HttpSession session) {
        CloudUser cloudUser = (CloudUser) session.getAttribute(SessionEnum.USER.getCode());
        RespEntity respEntity = cloudUserClient.userInfo(cloudUser.getId());
        return respEntity;
    }

    @DeleteMapping("/exit")
    @ApiOperation(value = "退出登录")
    public RespEntity exitLogin(HttpSession session) {
        session.removeAttribute(SessionEnum.USER.getCode());
        return new RespEntity(RespCode.SUCCESS);
    }

    @PostMapping("/upload")
    @ApiOperation(value = "上传图片")
    public RespEntity<String> upload(MultipartFile file){
        log.info("fileName={}", file.getOriginalFilename());
        String fileName = ossUtil.getFileName("cloud/head/", file.getOriginalFilename());
        String url = ossUtil.getUrl(fileName);
        try {
            byte[] bytes = file.getBytes();
            ossUtil.upload(fileName, bytes);
            return new RespEntity<>(RespCode.SUCCESS, url);
        } catch (Exception e) {
            log.error("图片上传失败", e.getMessage());
            throw new RuntimeException();
        }
    }

    @PutMapping("/update")
    @ApiOperation(value = "编辑个人信息")
    public RespEntity updateUser(UserParam userParam, HttpSession session) {
        CloudUser cloudUser = (CloudUser) session.getAttribute(SessionEnum.USER.getCode());
        userParam.setId(cloudUser.getId());
        RespEntity respEntity = cloudUserClient.updateUser(userParam);
        return respEntity;
    }

    @PostMapping("/checkPwd")
    @ApiOperation(value = "校验旧密码")
    public RespEntity checkPassword(UserPwdParam userPwdParam, HttpSession session) {
        CloudUser cloudUser = (CloudUser) session.getAttribute(SessionEnum.USER.getCode());
        userPwdParam.setId(cloudUser.getId());
        RespEntity respEntity = cloudUserClient.checkPassword(userPwdParam);
        return respEntity;
    }

    @PutMapping("/updatePwd")
    @ApiOperation(value = "修改密码")
    public RespEntity updatePassword(UserPwdParam userPwdParam, HttpSession session) {
        CloudUser cloudUser = (CloudUser) session.getAttribute(SessionEnum.USER.getCode());
        userPwdParam.setId(cloudUser.getId());
        RespEntity respEntity = cloudUserClient.updatePassword(userPwdParam);
        session.removeAttribute(SessionEnum.USER.getCode());
        return respEntity;
    }

    @PostMapping("/keep")
    @ApiOperation(value = "保持锁屏会话")
    public RespEntity keepSession(HttpSession session) {
        CloudUser cloudUser = (CloudUser) session.getAttribute(SessionEnum.USER.getCode());
        session.removeAttribute(SessionEnum.USER.getCode());
        session.removeAttribute(SessionEnum.BLOCK.getCode());
        session.setAttribute(SessionEnum.USER.getCode(), cloudUser);
        session.setAttribute(SessionEnum.BLOCK.getCode(), SessionEnum.BLOCK.getDesc());
        return new RespEntity(RespCode.SUCCESS);
    }

}
